var _util = require('util/util.js')
var vue = require('./../common/vue')
const { toHandlerKey } = require('vue');
const _market_service = require('service/market-service');

var coupon_content = new vue({
    el: "#coupon_content",
    data: {
        coupons: [{
            "couponid": 4,
            "shopname": "爱爸爸店",
            "fullminus": 50,
            "discount": 10,
            "stock": 1000,
            "avalaible": 0
        },
        {
            "couponid": 4,
            "shopname": "卷王服饰店",
            "fullminus": 50,
            "discount": 10,
            "stock": 1000,
            "avalaible": 1
        },
        {
            "couponid": 4,
            "shopname": "卷王服饰店",
            "fullminus": 50,
            "discount": 10,
            "stock": 1000,
            "avalaible": 0
        },
        {
            "couponid": 4,
            "shopname": "卷王服饰店",
            "fullminus": 50,
            "discount": 10,
            "stock": 1000,
            "avalaible": 1
        }
        ],
        pagesize : 6,
        pageid : 1,
        selectfullminus : "",
        selectshopname : "选择商铺",
        shopnames: ["自律数码店", "自律母婴店"],
        editstock: 0,
        editfullminus: 0,
        editdiscount: 0,
        editavailable: 1,
        editcouponid: 0
    },
    methods: {
        search: function () {
            console.log(this.selectshopname)
            console.log(this.selectfullminus)
            _market_service.getcouponsby(coupon_content.selectfullminus, coupon_content.selectshopname,
                function (res) {
                    console.log(res)
                    coupons = res;
                },
                function (err) {
                    console.log(err);
                })
        },
        seteidtmessage: function (index) {
            this.editstock = this.coupons[index].stock;
            this.editdiscount = this.coupons[index].discount;
            this.editavailable = this.coupons[index].avalaible? 1 : 0;
            this.editfullminus = this.coupons[index].fullminus;
            this.editcouponid = this.coupons[index].couponid;
            console.log(this.editavailable)
        },
        setavailable : function(index){
            this.editstock = this.coupons[index].stock;
            this.editdiscount = this.coupons[index].discount;
            this.editavailable = !this.coupons[index].avalaible? 1 : 0;
            this.editfullminus = this.coupons[index].fullminus;
            this.editcouponid = this.coupons[index].couponid;  
            console.log(this.editavailable);
            this.updatecoupon()
        },
        updatecoupon : function () {
            console.log("开始更新")
            _market_service.editcoupon(
                {
                    fullminus: coupon_content.editfullminus,
                    discount : coupon_content.editdiscount,
                    stock : coupon_content.editstock,
                    avalaible: coupon_content.editavailable
                },
                coupon_content.editcouponid,
                function (res) {
                    alert("保存成功")
                    console.log(res)
                },
                function (err) {
                    alert("保存失败")
                    console.log(err)
                }
            )
        },
        resetsearchinfo : function(){
            this.selectfullminus = "请输入使用金额下限";
            this.selectshopname = "选择商铺"
        }
    },
    //初始化vue对象，渲染数据
    created: function () {
        获取的购物车商品对象
        _market_service.getallcoupons(coupon_content.pagesize,coupon_content.pageid,
            function (res) {
                coupon_content.coupons = res;
            },
            function (res) {
                console.log("ERROR..*..")
            }
        );
    },

})